// pages/myPage/myPage.js
const util = require("../../utils/util")
const app = getApp()
import {authInfo} from '../../apis/user'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    userInfo: {},
    identity: '',
    userId: '',
    collectNum: 0,
    findTeaNum: 0,
    outTeaNum: 0,
    status:''
  },
  //去关于我们
  goAbout() {
    this.showSubscribeMessage()
  },
    /**
   * 判断是否订阅消息
   */
  showSubscribeMessage() {
    if (wx.getStorageSync('status')) return true
    return new Promise((resolve, reject) => {
      const id = app.globalData.subscribeMessage
      const that = this
      wx.requestSubscribeMessage({
        tmplIds: id,
        success(res) {
          wx.setStorageSync('status', true)
          that.setData({
            status:true
          })
          const f = id.every((item, index) => {
            if (res[item] != 'accept') {
              return false
            }
            return true
          })
          resolve(f)
        },
        fail(err) {
          app.globalData.subscribeState = true
          reject(true)
        }
      })
    })
  },
  //用户认证
  goUserAuth() {
    wx.navigateTo({
      url: '/pages/userAuth/userAuth',
    })
  },

  // 回收员资格申请
  goCollectorAuth() {
    if (this.data.userInfo.identity === 0) {
      wx.showToast({
        title: '请先进行认证',
        icon: 'none'
      })
      return ;
    }
    wx.navigateTo({
      url: '/pages/collectorAuth/collectorAuth',
    })
  },
  //去我的账单
  goOrder() {
    if (this.data.userInfo.identity === 0) {
      wx.showToast({
        title: '请先进行认证',
        icon: 'none'
      })
      return ;
    }
    wx.navigateTo({
      url: '/pages/myOrder/myOrder',
    })
  },

  goWish() {
    if (this.data.userInfo.identity === 0) {
      wx.showToast({
        title: '请先进行认证',
        icon: 'none'
      })
      return ;
    }
    wx.navigateTo({
      url: '/pages/wallet/wallet',
    })
  },

  goServe() {
    wx.openCustomerServiceChat({
      extInfo: {url: ''},
      corpId: '',
      success(res) {}
    })
  },
  //展示弹框
  showPopup() {
    this.setData({ show: true })
  },
  //关闭弹框
  onClose() {
    this.setData({ show: false })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取用户认证信息
    authInfo().then(res => {
      if (res.code == 200) {
        app.globalData.authInfo = res.data;
      } else {
        console.log(res.message);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.userId = wx.getStorageSync('token').id
    const user = JSON.parse(wx.getStorageSync('userInfo'))
    const status = wx.getStorageSync('status')
    let temp = "";
    if (user.identity == 0) {
      temp = "游客";
    } else if (user.identity == 1) {
      temp = "用户";
    } else if (user.identity == 2) {
      temp = "回收员";
    }
    this.setData({
      userInfo:user,
      status:status,
      identity: temp,
    })
  },

})